package figure8.GUI;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;
import javax.swing.border.EtchedBorder;

import figure8.Project;
import figure8.Seq;

public class SeqSaveWindow extends JDialog {
	
	public SeqSaveWindow(final SeqMainWindow window, final Seq seq) {
		super();
		
		this.setLocation(window.getPanel().getX() + 80, window.getPanel().getY() + 120);
		
		final JDialog seqWindow = this;	
		final JPanel panel = new JPanel(new GridLayout(2, 2, 2, 2));
		panel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
		
		this.getContentPane().add(panel);
		
		panel.add(new JLabel("Sequencer Name"));
		final JTextField name = new JTextField("Unnamed Sequencer");
		panel.add(name);

		final JButton submit = new JButton("Submit");
		submit.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser saveFC = new JFileChooser();
				saveFC.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				int returnVal = saveFC.showSaveDialog(window.getPanel());
				if (returnVal == JFileChooser.APPROVE_OPTION) {
					String dir = saveFC.getSelectedFile().getAbsolutePath();
					File projectDir = new File(dir + File.separator + name.getText());
					File directory = new File(dir);
					if(projectDir.exists())
					{
						SeqAlreadyExistsWindow seqExists = new SeqAlreadyExistsWindow(
								window, directory, name.getText(), seq);
					}
					else {
						projectDir.mkdir();
						seq.saveFile(projectDir);
					}
				} else
					System.out.println("Seq save window cancel button clicked");
				seqWindow.dispose();
			}
		});

		panel.add(submit);

		this.pack();
		this.setVisible(true);
	}
}